const deepClone = (val) => {
  const map = new Map()
  if(typeof val !== 'object') {
    return val
  } else if(val instanceof Array) {
    return clone(val, [], map)
  } else {
    return clone(val, {}, map)
  }
}
function clone(val, init, map) {
  let _val = map.get(val);
  //如果这个对象已经被记录则直接返回
  if (_val) {
    return _val;
  }
  _val = init
  map.set(val, _val)
  for(key in val) {
    if(typeof val[key] === 'object' && val[key] instanceof Array) {
      _val[key] = clone(val[key], [], map)
    } else if(typeof val[key] === 'object' && val[key] instanceof Object) {
      _val[key] = clone(val[key], {}, map)
    } else {
      _val[key] = val[key]
    }
  }
  return _val
}
export default deepClone

